package com.mju.utils;

import java.util.Calendar;
import java.util.Date;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

public class JWTUtils {	
	
	private static final String SECRET="lzm-mju&ept.2025@1";
	
	public static String genToken(String userInfoJson) {
		
		Calendar cal=Calendar.getInstance();
		Date now=cal.getTime(); 
		cal.add(Calendar.DATE, 30);//30天后过期	
		
		JWTCreator.Builder jwtBuilder=JWT.create();	
		return jwtBuilder.withSubject("工程化项目训练")
						 .withIssuer("lzm") .withClaim("userInfo", userInfoJson)
				         .withIssuedAt(now) .withExpiresAt(cal.getTime())
						 .sign(Algorithm.HMAC512(SECRET));		
	}
	
	public static DecodedJWT getTokenInfo(String token) {
		JWTVerifier verifier=JWT.require(Algorithm.HMAC512(SECRET)).build();		
		try {
			DecodedJWT decodedJWT=verifier.verify(token);
			return decodedJWT;
		}catch(Exception ex) {
			return null; 
		}						
	}
}
